home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / hobbes3 / ellipses.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-17  |  11.2 KB  |  507 lines

  1. #include "hobbes.h"
  2.  
  3. void Plot4(int x, int y, int a, int b, COLOR c)
  4. {
  5.     int x1,x2, y1,y2;
  6.     x1 = x-a;
  7.     x2 = x+a;
  8.     y1 = y-b;
  9.     y2 = y+b;
  10.  
  11.     Pixel(x1,y1, c);
  12.     if (x1!=x2)
  13.         Pixel(x2,y1,c);
  14.     if (y1!=y2) {
  15.         Pixel(x1,y2,c);
  16.         if (x1!=x2)
  17.             Pixel(x2,y2,c);
  18.     }
  19. }
  20.  
  21. void Plot4Clip(int x, int y, int a, int b, COLOR c)
  22. {
  23.     int x1,x2, y1,y2;
  24.     x1 = x-a;
  25.     x2 = x+a;
  26.     y1 = y-b;
  27.     y2 = y+b;
  28.  
  29.     PixelClip(x1,y1, c);
  30.     if (x1!=x2)
  31.         PixelClip(x2,y1,c);
  32.     if (y1!=y2) {
  33.         PixelClip(x1,y2,c);
  34.         if (x1!=x2)
  35.             PixelClip(x2,y2,c);
  36.     }
  37. }
  38.  
  39.  
  40. void Plot4Fill(int x, int y, int a, int b, COLOR c)
  41. {
  42.     int x1,x2, y1,y2;
  43.     x1 = x-a;
  44.     x2 = x+a;
  45.     y1 = y-b;
  46.     y2 = y+b;
  47.  
  48.     HLine(x1,x2,y1,c);
  49.     if (y1!=y2)
  50.         HLine(x1,x2,y2,c);
  51. }
  52.  
  53. void Plot4FillClip(int x, int y, int a, int b, COLOR c)
  54. {
  55.     int x1,x2, y1,y2;
  56.     x1 = x-a;
  57.     x2 = x+a;
  58.     y1 = y-b;
  59.     y2 = y+b;
  60.  
  61.     HLineClip(x1,x2,y1,c);
  62.     if (y1!=y2)
  63.         HLineClip(x1,x2,y2,c);
  64. }
  65.  
  66.  
  67. void Plot4Pattern(int x, int y, int a, int b, PATTERN p)
  68. {
  69.     int x1,x2, y1,y2;
  70.     x1 = x-a;
  71.     x2 = x+a;
  72.     y1 = y-b;
  73.     y2 = y+b;
  74.  
  75.     PixelPattern(x1,y1,p);
  76.     if (x1!=x2)
  77.         PixelPattern(x2,y1,p);
  78.     if (y1!=y2) {
  79.         PixelPattern(x1,y2,p);
  80.         if (x1!=x2)
  81.             PixelPattern(x2,y2,p);
  82.     }
  83. }
  84.  
  85. void Plot4PatternClip(int x, int y, int a, int b, PATTERN p)
  86. {
  87.     int x1,x2, y1,y2;
  88.     x1 = x-a;
  89.     x2 = x+a;
  90.     y1 = y-b;
  91.     y2 = y+b;
  92.     PixelPatternClip(x1,y1,p);
  93.     if (x1!=x2)
  94.         PixelPatternClip(x2,y1,p);
  95.     if (y1!=y2) {
  96.         PixelPatternClip(x1,y2,p);
  97.         if (x1!=x2)
  98.             PixelPatternClip(x2,y2,p);
  99.     }
  100.  
  101. }
  102.  
  103. void Plot4FillPattern(int x, int y, int a, int b, PATTERN p)
  104. {
  105.     int x1,x2, y1,y2;
  106.     x1 = x-a;
  107.     x2 = x+a;
  108.     y1 = y-b;
  109.     y2 = y+b;
  110.  
  111.     HLinePattern(x1,x2,y1,p);
  112.     if (y1!=y2)
  113.         HLinePattern(x1,x2,y2,p);
  114. }
  115.  
  116. void Plot4FillPatternClip(int x, int y, int a, int b, PATTERN p)
  117. {
  118.     int x1,x2, y1,y2;
  119.     x1 = x-a;
  120.     x2 = x+a;
  121.     y1 = y-b;
  122.     y2 = y+b;
  123.  
  124.     HLinePatternClip(x1,x2,y1,p);
  125.     if (y1!=y2)
  126.         HLinePatternClip(x1,x2,y2,p);
  127. }
  128.  
  129.  
  130. /*********************************************************************
  131.  * Draw ellipse with center at x,y, horiz. radius a and vert. radius b
  132.  * The algorithm is from "An Efficient Ellipse-Drawing Algorithm" by
  133.  * Jerry R. Van Aken, IEEE CG&A, September 1984, pp. 24-35,
  134.  * specifically, Figure 10 on page 32.
  135.  *
  136.  * FOR SOME REASON THIS FUNCTION FAILS IF a OR b IS LARGER THAN 255
  137.  ********************************************************************/
  138.  
  139. void Ellipse(int centerx, int centery, int a, int b, COLOR c)
  140. {
  141.     register long   d1, d2;
  142.     register int    x, y;
  143.     register long   t1, t2, t3, t4, t5, t6, t7, t8, t9;
  144.  
  145.     /* intermediate terms to speed up loop */
  146.     t1 = a * a; t2 = t1 << 1;   t3 = t2 << 1;
  147.     t4 = b * b; t5 = t4 << 1;   t6 = t5 << 1;
  148.     t7 = a * t5;    t8 = t7 << 1;   t9 = 0L;
  149.  
  150.     d1 = t2 - t7 + (t4 >> 1);   /* error terms */
  151.     d2 = (t1 >> 1) - t8 + t5;
  152.  
  153.     x = a;
  154.     y = 0;
  155.  
  156.     while (d2 < 0) {        /* region 1 of ellipse */
  157.         Plot4(centerx, centery, x, y, c);
  158.         y++;        /* always move up here */
  159.         t9 += t3;
  160.         if (d1 < 0) {       /* move straight up */
  161.             d1 += t9 + t2;
  162.             d2 += t9;
  163.         } else {        /* move up and left */
  164.             x--;
  165.             t8 -= t6;
  166.             d1 += t9 + t2 - t8;
  167.             d2 += t9 + t5 - t8;
  168.         }
  169.     }
  170.  
  171.     do {                /* region 2 of ellipse */
  172.         Plot4(centerx, centery, x, y, c);
  173.         x--;        /* always move left here */
  174.         t8 -= t6;
  175.         if (d2 < 0) {   /* move up and left */
  176.             y++;
  177.             t9 += t3;
  178.             d2 += t9 + t5 - t8;
  179.         } else      /* move straight left */
  180.             d2 += t5 - t8;
  181.     } while (x >= 0);
  182. }
  183.  
  184. void EllipseClip(int centerx, int centery, int a, int b, COLOR c)
  185. {
  186.     register long   d1, d2;
  187.     register int    x, y;
  188.     register long   t1, t2, t3, t4, t5, t6, t7, t8, t9;
  189.  
  190.     /* intermediate terms to speed up loop */
  191.     t1 = a * a; t2 = t1 << 1;   t3 = t2 << 1;
  192.     t4 = b * b; t5 = t4 << 1;   t6 = t5 << 1;
  193.     t7 = a * t5;    t8 = t7 << 1;   t9 = 0L;
  194.  
  195.     d1 = t2 - t7 + (t4 >> 1);   /* error terms */
  196.     d2 = (t1 >> 1) - t8 + t5;
  197.  
  198.     x = a;
  199.     y = 0;
  200.  
  201.     while (d2 < 0) {        /* region 1 of ellipse */
  202.         Plot4Clip(centerx, centery, x, y, c);
  203.         y++;        /* always move up here */
  204.         t9 += t3;
  205.         if (d1 < 0) {       /* move straight up */
  206.             d1 += t9 + t2;
  207.             d2 += t9;
  208.         } else {        /* move up and left */
  209.             x--;
  210.             t8 -= t6;
  211.             d1 += t9 + t2 - t8;
  212.             d2 += t9 + t5 - t8;
  213.         }
  214.     }
  215.  
  216.     do {                /* region 2 of ellipse */
  217.         Plot4Clip(centerx, centery, x, y, c);
  218.         x--;        /* always move left here */
  219.         t8 -= t6;
  220.         if (d2 < 0) {   /* move up and left */
  221.             y++;
  222.             t9 += t3;
  223.             d2 += t9 + t5 - t8;
  224.         } else      /* move straight left */
  225.             d2 += t5 - t8;
  226.     } while (x >= 0);
  227. }
  228.  
  229.  
  230.  
  231. void EllipseFill(int centerx, int centery, int a, int b, COLOR c)
  232. {
  233.     register long   d1, d2;
  234.     register int    x, y;
  235.     register long   t1, t2, t3, t4, t5, t6, t7, t8, t9;
  236.  
  237.     /* intermediate terms to speed up loop */
  238.     t1 = a * a; t2 = t1 << 1;   t3 = t2 << 1;
  239.     t4 = b * b; t5 = t4 << 1;   t6 = t5 << 1;
  240.     t7 = a * t5;    t8 = t7 << 1;   t9 = 0L;
  241.  
  242.     d1 = t2 - t7 + (t4 >> 1);   /* error terms */
  243.     d2 = (t1 >> 1) - t8 + t5;
  244.  
  245.     x = a;
  246.     y = 0;
  247.  
  248.     while (d2 < 0) {        /* region 1 of ellipse */
  249.         Plot4Fill(centerx, centery, x, y, c);
  250.         y++;        /* always move up here */
  251.         t9 += t3;
  252.         if (d1 < 0) {       /* move straight up */
  253.             d1 += t9 + t2;
  254.             d2 += t9;
  255.         } else {        /* move up and left */
  256.             x--;
  257.             t8 -= t6;
  258.             d1 += t9 + t2 - t8;
  259.             d2 += t9 + t5 - t8;
  260.         }
  261.     }
  262.  
  263.     do {                /* region 2 of ellipse */
  264.         x--;        /* always move left here */
  265.         t8 -= t6;
  266.         if (d2 < 0) {   /* move up and left */
  267.             Plot4Fill(centerx, centery, x, y, c);
  268.             y++;
  269.             t9 += t3;
  270.             d2 += t9 + t5 - t8;
  271.         } else      /* move straight left */
  272.             d2 += t5 - t8;
  273.     } while (x >= 0);
  274. }
  275.  
  276. void EllipseFillClip(int centerx, int centery, int a, int b, COLOR c)
  277. {
  278.     register long   d1, d2;
  279.     register int    x, y;
  280.     register long   t1, t2, t3, t4, t5, t6, t7, t8, t9;
  281.  
  282.     /* intermediate terms to speed up loop */
  283.     t1 = a * a; t2 = t1 << 1;   t3 = t2 << 1;
  284.     t4 = b * b; t5 = t4 << 1;   t6 = t5 << 1;
  285.     t7 = a * t5;    t8 = t7 << 1;   t9 = 0L;
  286.  
  287.     d1 = t2 - t7 + (t4 >> 1);   /* error terms */
  288.     d2 = (t1 >> 1) - t8 + t5;
  289.  
  290.     x = a;
  291.     y = 0;
  292.  
  293.     while (d2 < 0) {        /* region 1 of ellipse */
  294.         Plot4FillClip(centerx, centery, x, y, c);
  295.         y++;        /* always move up here */
  296.         t9 += t3;
  297.         if (d1 < 0) {       /* move straight up */
  298.             d1 += t9 + t2;
  299.             d2 += t9;
  300.         } else {        /* move up and left */
  301.             x--;
  302.             t8 -= t6;
  303.             d1 += t9 + t2 - t8;
  304.             d2 += t9 + t5 - t8;
  305.         }
  306.     }
  307.  
  308.     do {                /* region 2 of ellipse */
  309.         x--;        /* always move left here */
  310.         t8 -= t6;
  311.         if (d2 < 0) {   /* move up and left */
  312.             Plot4FillClip(centerx, centery, x, y, c);
  313.             y++;
  314.             t9 += t3;
  315.             d2 += t9 + t5 - t8;
  316.         } else      /* move straight left */
  317.             d2 += t5 - t8;
  318.     } while (x >= 0);
  319. }
  320.  
  321.  
  322. void EllipsePattern(int centerx, int centery, int a, int b, PATTERN p)
  323. {
  324.     register long   d1, d2;
  325.     register int    x, y;
  326.     register long   t1, t2, t3, t4, t5, t6, t7, t8, t9;
  327.  
  328.     /* intermediate terms to speed up loop */
  329.     t1 = a * a; t2 = t1 << 1;   t3 = t2 << 1;
  330.     t4 = b * b; t5 = t4 << 1;   t6 = t5 << 1;
  331.     t7 = a * t5;    t8 = t7 << 1;   t9 = 0L;
  332.  
  333.     d1 = t2 - t7 + (t4 >> 1);   /* error terms */
  334.     d2 = (t1 >> 1) - t8 + t5;
  335.  
  336.     x = a;
  337.     y = 0;
  338.  
  339.     while (d2 < 0) {        /* region 1 of ellipse */
  340.         Plot4Pattern(centerx, centery, x, y, p);
  341.         y++;        /* always move up here */
  342.         t9 += t3;
  343.         if (d1 < 0) {       /* move straight up */
  344.             d1 += t9 + t2;
  345.             d2 += t9;
  346.         } else {        /* move up and left */
  347.             x--;
  348.             t8 -= t6;
  349.             d1 += t9 + t2 - t8;
  350.             d2 += t9 + t5 - t8;
  351.         }
  352.     }
  353.  
  354.     do {                /* region 2 of ellipse */
  355.         Plot4Pattern(centerx, centery, x, y, p);
  356.         x--;        /* always move left here */
  357.         t8 -= t6;
  358.         if (d2 < 0) {   /* move up and left */
  359.             y++;
  360.             t9 += t3;
  361.             d2 += t9 + t5 - t8;
  362.         } else      /* move straight left */
  363.             d2 += t5 - t8;
  364.     } while (x >= 0);
  365. }
  366.  
  367.  
  368. void EllipsePatternClip(int centerx, int centery, int a, int b, PATTERN p)
  369. {
  370.     register long   d1, d2;
  371.     register int    x, y;
  372.     register long   t1, t2, t3, t4, t5, t6, t7, t8, t9;
  373.  
  374.     /* intermediate terms to speed up loop */
  375.     t1 = a * a; t2 = t1 << 1;   t3 = t2 << 1;
  376.     t4 = b * b; t5 = t4 << 1;   t6 = t5 << 1;
  377.     t7 = a * t5;    t8 = t7 << 1;   t9 = 0L;
  378.  
  379.     d1 = t2 - t7 + (t4 >> 1);   /* error terms */
  380.     d2 = (t1 >> 1) - t8 + t5;
  381.  
  382.     x = a;
  383.     y = 0;
  384.  
  385.     while (d2 < 0) {        /* region 1 of ellipse */
  386.         Plot4PatternClip(centerx, centery, x, y, p);
  387.         y++;        /* always move up here */
  388.         t9 += t3;
  389.         if (d1 < 0) {       /* move straight up */
  390.             d1 += t9 + t2;
  391.             d2 += t9;
  392.         } else {        /* move up and left */
  393.             x--;
  394.             t8 -= t6;
  395.             d1 += t9 + t2 - t8;
  396.             d2 += t9 + t5 - t8;
  397.         }
  398.     }
  399.  
  400.     do {                /* region 2 of ellipse */
  401.         Plot4PatternClip(centerx, centery, x, y, p);
  402.         x--;        /* always move left here */
  403.         t8 -= t6;
  404.         if (d2 < 0) {   /* move up and left */
  405.             y++;
  406.             t9 += t3;
  407.             d2 += t9 + t5 - t8;
  408.         } else      /* move straight left */
  409.             d2 += t5 - t8;
  410.     } while (x >= 0);
  411. }
  412.  
  413.  
  414.  
  415.  
  416. void EllipseFillPattern(int centerx, int centery, int a, int b, PATTERN p)
  417. {
  418.     register long   d1, d2;
  419.     register int    x, y;
  420.     register long   t1, t2, t3, t4, t5, t6, t7, t8, t9;
  421.  
  422.     /* intermediate terms to speed up loop */
  423.     t1 = a * a; t2 = t1 << 1;   t3 = t2 << 1;
  424.     t4 = b * b; t5 = t4 << 1;   t6 = t5 << 1;
  425.     t7 = a * t5;    t8 = t7 << 1;   t9 = 0L;
  426.  
  427.     d1 = t2 - t7 + (t4 >> 1);   /* error terms */
  428.     d2 = (t1 >> 1) - t8 + t5;
  429.  
  430.     x = a;
  431.     y = 0;
  432.  
  433.     while (d2 < 0) {        /* region 1 of ellipse */
  434.         Plot4FillPattern(centerx, centery, x, y, p);
  435.         y++;        /* always move up here */
  436.         t9 += t3;
  437.         if (d1 < 0) {       /* move straight up */
  438.             d1 += t9 + t2;
  439.             d2 += t9;
  440.         } else {        /* move up and left */
  441.             x--;
  442.             t8 -= t6;
  443.             d1 += t9 + t2 - t8;
  444.             d2 += t9 + t5 - t8;
  445.         }
  446.     }
  447.  
  448.     do {                /* region 2 of ellipse */
  449.         Plot4FillPattern(centerx, centery, x, y, p);
  450.         x--;        /* always move left here */
  451.         t8 -= t6;
  452.         if (d2 < 0) {   /* move up and left */
  453.             y++;
  454.             t9 += t3;
  455.             d2 += t9 + t5 - t8;
  456.         } else      /* move straight left */
  457.             d2 += t5 - t8;
  458.     } while (x >= 0);
  459. }
  460.  
  461.  
  462. void EllipseFillPatternClip(int centerx, int centery, int a, int b, PATTERN p)
  463. {
  464.     register long   d1, d2;
  465.     register int    x, y;
  466.     register long   t1, t2, t3, t4, t5, t6, t7, t8, t9;
  467.  
  468.     /* intermediate terms to speed up loop */
  469.     t1 = a * a; t2 = t1 << 1;   t3 = t2 << 1;
  470.     t4 = b * b; t5 = t4 << 1;   t6 = t5 << 1;
  471.     t7 = a * t5;    t8 = t7 << 1;   t9 = 0L;
  472.  
  473.     d1 = t2 - t7 + (t4 >> 1);   /* error terms */
  474.     d2 = (t1 >> 1) - t8 + t5;
  475.  
  476.     x = a;
  477.     y = 0;
  478.  
  479.     while (d2 < 0) {        /* region 1 of ellipse */
  480.         Plot4FillPatternClip(centerx, centery, x, y, p);
  481.         y++;        /* always move up here */
  482.         t9 += t3;
  483.         if (d1 < 0) {       /* move straight up */
  484.             d1 += t9 + t2;
  485.             d2 += t9;
  486.         } else {        /* move up and left */
  487.             x--;
  488.             t8 -= t6;
  489.             d1 += t9 + t2 - t8;
  490.             d2 += t9 + t5 - t8;
  491.         }
  492.     }
  493.  
  494.     do {                /* region 2 of ellipse */
  495.         Plot4FillPatternClip(centerx, centery, x, y, p);
  496.         x--;        /* always move left here */
  497.         t8 -= t6;
  498.         if (d2 < 0) {   /* move up and left */
  499.             y++;
  500.             t9 += t3;
  501.             d2 += t9 + t5 - t8;
  502.         } else      /* move straight left */
  503.             d2 += t5 - t8;
  504.     } while (x >= 0);
  505. }
  506.  
  507.